import ClientLibrary.Board;
import ClientLibrary.Network.NetLayer;
import ClientLibrary.UI.MainForm;
import ClientLibrary.UI.MenuForm;
import CommonLibrary.Commnads.*;
import CommonLibrary.Point;

import javax.swing.border.Border;

public class Receiver extends Thread{
    
    private NetLayer netLayer;
    private Command command;

    private Board board = null;
    
    public Receiver(NetLayer layer){
        this.netLayer = layer;
        this.command = null;
    }
    
    public void run() {
        while(true)
        {
            try
            {
                this.command = this.netLayer.startReceivingCommand();
                this.HandleCommand(this.command);
            }
            catch (Exception error)
            {
                //System.out.print(error.getMessage());
            }
        }
    }

    private void HandleCommand(Command command){
        switch (command.getId())
        {
            case 1:
            {
                this.HandleCommand((AuthorizeResponse)command);
                break;
            }
            case 9:
            {
                this.HandleCommand((ListResponse)command);
                break;
            }
            case 11:
            {
                this.HandleCommand((JoinGameResponse)command);
                break;
            }
            case 13:
            {
                this.HandleCommand((CreateGameResponse)command);
                break;
            }
            case 15:
            {
                this.HandleCommand((EnemyMoveResponse) command);
                break;
            }

            case 5:
            {
                this.HandleCommand((MoveResponse)command);
                break;
            }
        }
    }

    private void HandleCommand(AuthorizeResponse authCommand)
    {
        if(authCommand.getCheckAuthorize() != 0)
        {
            Board.getBoard().setPlayerId(authCommand.getPlayerId());
            try
            {
                this.netLayer.sendCommand(new ListRequest());
            }
            catch (Exception error)
            {
                System.out.print(error.getMessage());
            }
        }
        else
        {

            // TODO
        }
    }

    private void HandleCommand(ListResponse listCommand)
    {
        new MenuForm(
                listCommand.getGamesID() == null ? new int[0] : listCommand.getGamesID(),
                listCommand.getNames() == null ? new String[0] : listCommand.getNames(),
                listCommand.getSides() == null ? new int[0] : listCommand.getSides(),
                this.netLayer
                    ).paintFrame();
    }
    
    private void HandleCommand(JoinGameResponse joinResponse)
    {
        if(joinResponse.getJoiningSuccess() != 0)
        {
            Board.getBoard().StartGame();
        }
        else
        {
            // TODO
        }
    }
    
    private void HandleCommand(CreateGameResponse createGameResponse)
    {
        if(createGameResponse.getGameCreated() != 0)
        {
            Board.getBoard().setGameId(createGameResponse.getGameID());
            Board.getBoard().StartGame();
        }
    }

    public void HandleCommand(EnemyMoveResponse enemyMoveResponse)
    {
        if(Board.getBoard().getSide() == 0)
        {
            for(Point p: enemyMoveResponse.getStep())
            {
                p.setX(7 - p.getX());
                p.setY(7 - p.getY());
            }
        }
        Board.getBoard().move(enemyMoveResponse.getStep());
    }

    public void HandleCommand(MoveResponse moveResponse)
    {
        if(moveResponse.getCheckMove() != 1)
        {
            System.exit(0);
        }
        else
        {
            EnemyMoveRequest request = new EnemyMoveRequest();
            request.setGameID(Board.getBoard().getGameId());
            request.setPlayerID(Board.getBoard().getPlayerId());
            try
            {
                this.netLayer.sendCommand(request);
            }
            catch (Exception error)
            {
                System.out.print("\nCan't send EnemyMoveRequest\n");
            }
        }
    }
}
